package com.servlet.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.MD5;
import com.servlet.dao.UserDao;
import com.servlet.service.UserService;
import com.servlet.vo.User;

public class UserAction extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter pw = response.getWriter();
		String op = request.getParameter("op");
		try {
			if(null != op){
				if("AD".equals(op)){// 添加
					
				}else if("UP".equals(op)){// 修改
					
				}else if("DE".equals(op)){// 删除
					
				}else if("RP".equals(op)){// 重置密码
					String npassw = request.getParameter("npw");
					String opassw = request.getParameter("opw");
					String userName = request.getParameter("un");
					
					try {
						User user = new UserDao().getLoadUser(userName, opassw);
						if(null != user){
							user.setPassword(MD5.getCCBMd5(npassw));
							boolean flag = new UserDao().update(user);
							if(flag){
								pw.write(user.getUserName() + "密码修改成功");
							}else{
								pw.write(userName + "密码修改失败");
							}
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					
				}else if("QU".equals(op)){// 退出
					request.getSession().removeAttribute("USERSESSION");
					response.sendRedirect("load.jsp");
				}else if("LO".equals(op)){// 登陆
					
					boolean load = false;
					try {
						load = new UserService().load(request);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if(load){
						// load success
						//request.getRequestDispatcher("/frontpage/main.jsp").forward(request, response);
						response.sendRedirect("AccountAction?op=SH");
					}else{
						// load error
						response.sendRedirect("load.jsp");
					}
				}
				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(pw != null){
				pw.close();
			}
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
